package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import modle.Country;

import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

public class Admin_Menu extends JFrame {

	private JPanel contentPane;
	private JComboBox comboBox;
	private JLabel welLabel;
	private JLabel ma_Label;
	private JButton btnAddMenu;
	private JButton btnRemoveMenu;
	private JButton btnExit;
	private JButton btnRegisterAdmin;
	private JButton btnUpdateMenu;
	private JButton btnViewItems;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;						// 资源束包含区域特定的对象。
	private JLabel backgroundlabel;
	private JButton btnBack;
	private JButton btnViewOrders;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin_Menu frame = new Admin_Menu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Admin_Menu() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Admin_Menu.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 701, 756);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		welLabel = new JLabel("Welcome to Admin Module ", JLabel.CENTER);
		welLabel.setForeground(new Color(205, 133, 63));
		welLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 23));
		welLabel.setBounds(169, 13, 352, 39);
		contentPane.add(welLabel);
		
		ma_Label = new JLabel("Menu for Admin", JLabel.CENTER);
		ma_Label.setForeground(new Color(70, 130, 180));
		ma_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		ma_Label.setBounds(244, 152, 196, 26);
		contentPane.add(ma_Label);
		
		btnAddMenu = new JButton("Add New Item in Menu");
		btnAddMenu.setForeground(new Color(70, 130, 180));
		btnAddMenu.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnAddMenu.setBounds(189, 207, 300, 48);
		contentPane.add(btnAddMenu);
		btnAddMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddMenu().setVisible(true);					//打开增加餐单窗口
				dispose();
			}
		});
		
		btnUpdateMenu = new JButton("Update the Item");
		btnUpdateMenu.setForeground(new Color(70, 130, 180));
		btnUpdateMenu.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnUpdateMenu.setBounds(189, 284, 300, 48);
		contentPane.add(btnUpdateMenu);
		btnUpdateMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new UpdateMenu().setVisible(true);					//打开更新餐单窗口
				dispose();
			}
		});
		
		btnRemoveMenu = new JButton("Remove Item");
		btnRemoveMenu.setForeground(new Color(70, 130, 180));
		btnRemoveMenu.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnRemoveMenu.setBounds(189, 358, 300, 48);
		contentPane.add(btnRemoveMenu);
		btnRemoveMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new DelMenu().setVisible(true);					//打开更新餐单窗口
				dispose();
			}
		});
		
		btnExit = new JButton("Exit");
		btnExit.setForeground(new Color(0, 0, 0));
		btnExit.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		btnExit.setBounds(571, 640, 87, 39);
		contentPane.add(btnExit);
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setIcon(new ImageIcon(Admin_Menu.class.getResource("/img/admin2.png")));
		lblNewLabel_2.setBounds(314, 50, 74, 99);
		contentPane.add(lblNewLabel_2);
		
		btnRegisterAdmin = new JButton("Add Admin");					//相当于注册功能
		btnRegisterAdmin.setForeground(new Color(70, 130, 180));
		btnRegisterAdmin.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnRegisterAdmin.setBounds(189, 587, 300, 48);
		contentPane.add(btnRegisterAdmin);
		btnRegisterAdmin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Register().setVisible(true);
				dispose();
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(571, 18, 98, 32);
		contentPane.add(comboBox);
		
		btnBack = new JButton("Back");
		btnBack.setForeground(Color.BLACK);
		btnBack.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
		btnBack.setBounds(14, 640, 87, 39);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Login().setVisible(true);
				dispose();
			}
		});
		
		btnViewOrders = new JButton("View Orders");
		btnViewOrders.setForeground(new Color(70, 130, 180));
		btnViewOrders.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnViewOrders.setBounds(189, 512, 300, 48);
		contentPane.add(btnViewOrders);
		btnViewOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new View_Orders().setVisible(true);
				dispose();
			}
		});
		
		btnViewItems = new JButton("View Items");
		btnViewItems.setForeground(new Color(70, 130, 180));
		btnViewItems.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		btnViewItems.setBounds(189, 437, 300, 48);
		contentPane.add(btnViewItems);
		btnViewItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new View_Items().setVisible(true);
				dispose();
			}
		});
		
		fillCountries();
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(Admin_Menu.class.getResource(imgpath));
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		btnViewItems.setText(rd.getString("btnViewItems"));
		welLabel.setText(rd.getString("welLabel"));
		ma_Label.setText(rd.getString("ma_Label"));
		btnAddMenu.setText(rd.getString("btnAddMenu"));
		btnUpdateMenu.setText(rd.getString("btnUpdateMenu"));
		btnRemoveMenu.setText(rd.getString("btnRemoveMenu"));
		btnRegisterAdmin.setText(rd.getString("btnRegisterAdmin"));
		btnViewOrders.setText(rd.getString("btnViewOrders"));
		btnExit.setText(rd.getString("btnExit"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
